var product = (function ($) {
    var _self = {
        scrollTop: 0,
        setScrollTop: function (top) {
            this.scrollTop = parseInt(top)
        },
        dom: $('#model li'),
        getProductParams: {
            current_page: 1,
            page_size: 10,
            contentid: [0],
            name: '',
        },
        canSend: true,
        nodata: false,
        // ==============================init start =============================
        __init__: function () {
            var self = this
            this.getProduct()
            this.getContent()
            $('#all').on('click', function () {
                _self.checkContentid('0')
                _self.getProductParams.current_page = 1
                _self.getProduct(true)
            })
            $('#footer').load('../../directive/footer.html', function() {
                var img  = $('#footer .weui-tabbar a').eq(1).find('img')
                img.attr('src', img.attr('src').replace('normal', 'hover'))
                img.parent().addClass('weui-bar__item_on')
            })
            
            $('#search_text').on('click', function () {
                $('.weui-search-bar').addClass('weui-search-bar_focusing')
            })

            $('#search_cancel').on('click', function () {
                $('.weui-search-bar').removeClass('weui-search-bar_focusing')
                $('#search_input').val('')
                self.getProductParams.current_page = 1
                self.getProductParams.name = ''
                self.getProduct(true)
            })

            $('#search_clear').on('click', function () {
                $('#search_input').val('')
            })

            $('#search_form').on('submit', function (e) {
                e.preventDefault()
                _self.getProductParams.current_page = 1
                if(!$('#search_input').val()){
                    return
                }
                _self.getProductParams.name = $('#search_input').val()
                _self.getProduct(true)
            })

            $('#box').on('scroll', function () {
                var flag = $(this)[0].scrollTop < _self.scrollTop ? false : true
                _self.setScrollTop($(this)[0].scrollTop)
                if(flag) {
                    var dic =  parseInt($('.small').height()) - (parseInt($('#box').height()) + parseInt($(this)[0].scrollTop))
                    if(dic < 3) {
                        if(_self.canSend && !_self.nodata) {
                            _self.getProductParams.current_page += 1
                            _self.getProduct(false)
                        }
                    }
                }
                
            })
        }
        // ===================init over====================
    }
    _self.setContainer = function (data, domin) {
        data.forEach(function (e, index) {
            var $dom = _self.dom.clone()
            var rightHeight = $('#right').height()
            var leftHeight = $('#left').height()
            $dom.attr('data-id', e.id)
            $dom.find('.product_img').attr('src', domin + e.img.split(',')[0])
            $dom.find('.product_name span').html('#' + e.id + ' ' + e.name)
            $dom.find('.product_remark span').html(e.remark)
            $dom.find('.product_num span').html(e.id)
            $dom.find('a').attr('href', '../detail/detail.html?id=' + e.id)
            $dom.appendTo('#' + (leftHeight > rightHeight ? 'right' : 'left'))
        })
    }

    _self.getProduct = function (f) {
        if(f) {
            $('#left').empty()
            $('#right').empty()
            _self.nodata = false
        }
        if(_self.nodata) {
            return
        } 
        _self.getProductParams.contentid = _self.getProductParams.contentid.join(',')
        $.ajax({
            url: '/api/product/query_product',
            data: this.getProductParams,
            type: 'post',
            dataType: 'json',
            beforeSend: function () {
                _self.canSend = false
                $('#loading').show()
                $('#nodata').hide()
            },
            success: function (res) {
                _self.getProductParams.contentid = _self.getProductParams.contentid.split(',')
                _self.refreshDom()
                _self.canSend = true
                $('#loading').hide()
                if(!res.data.length){
                    _self.nodata = true
                    $('#nodata').show()
                }
                _self.setContainer(res.data, res.object.domin)
            },
            error: function (error) {
                _self.getProductParams.contentid = _self.getProductParams.contentid.split(',')
            }
        })
    }
    _self.checkContentid = function (id) {
        if (id == 0){
            _self.getProductParams.contentid = [0]
        }else{
            var oindex = _self.getProductParams.contentid.indexOf('0')
            if (oindex !== -1) {
                _self.getProductParams.contentid.splice(oindex, 1)
            }
            _self.getProductParams.contentid.some(function (con) {
                return parseInt(con) === parseInt(id)
            }) ? _self.getProductParams.contentid =  _self.getProductParams.contentid.filter(function (con) {
                return con != id
            }) : _self.getProductParams.contentid.push(id)

            if (!_self.getProductParams.contentid.length) {
                _self.getProductParams.contentid = [0]
            }
        }
    }
    _self.refreshDom = function () {
        $('.button-sp-area a').each(function (ind, val) {
            _self.getProductParams.contentid.indexOf(String($(val).attr('key'))) !== -1 ? 
                $(val).addClass('weui-btn_primary').removeClass('weui-btn_default') :
                $(val).addClass('weui-btn_default').removeClass('weui-btn_primary')
        })
        if (_self.getProductParams.contentid.length === 1) {
            
        }else{

            
        }
    }
    _self.getContent = function () {
        $.ajax({
            url: '/api/config/query_content',
            data: {},
            dataType: 'json',
            type: 'post',
            success: function (res) {
                if (res.code === 0){
                    res.data.forEach(function (content) {
                        var a = document.createElement('a')
                        a.className = 'weui-btn weui-btn_mini weui-btn_default'
                        a.innerHTML = content.name
                        a.setAttribute('key', content.id)
                        a.onclick = (function (id) {
                            return function () {
                                _self.checkContentid(id)
                                _self.getProductParams.current_page = 1
                                _self.getProduct(true)
                            }
                        }(content.id))
                        $(a).appendTo($('.button-sp-area'))
                    })
                }
            }
        })
    }

    return _self
})($)

product.__init__()